package com.software.levelupandroid.systemTask.model;

import android.content.Context;

import androidx.annotation.NonNull;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.software.levelupandroid.entity.Badge;
import com.software.levelupandroid.entity.Result;
import com.software.levelupandroid.entity.SystemTask;
import com.software.levelupandroid.home.CallBack;
import com.software.levelupandroid.utils.OkhttpUtil;

import java.io.IOException;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.MediaType;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;

/**
 * @Description TODO
 * @Author LiangYaJie
 * @CreateTime 2024年05月29日 11:32:45
 */
public class SystemShowRepository {
    private static SystemShowRepository systemShowRepository;
    private List<SystemTask> tasks = new ArrayList<>();

    public static SystemShowRepository getSystemShowRepository(){
        if (systemShowRepository == null){
            systemShowRepository = new SystemShowRepository();
        }
        return systemShowRepository;
    }

    public void getAllTasksByStatus(CallBack<List<SystemTask>> callBack, Context context, int type){
        Call call = OkhttpUtil.getRequest("systemTask/getSystemTasksByType?type=" + type, context);

        call.enqueue(new Callback() {
            @Override
            public void onFailure(@NonNull Call call, @NonNull IOException e) {
                e.printStackTrace();
                callBack.onFailure(e.getMessage());
            }

            @Override
            public void onResponse(@NonNull Call call, @NonNull Response response) throws IOException {
                String resp = response.body().string();

                Result<List<SystemTask>> result = convertJsonToTaskList(resp);

                if (result != null){
                    if (result.getCode() != null && result.getCode().equals("200")){
                        List<SystemTask> tasks = result.getData();
                        callBack.onSuccess(tasks);
                    }else {
                        callBack.onFailure("result.getCode==null||result.getCode.equals('200'): 获取任务失败");
                    }
                }else {
                    callBack.onFailure("result==null: 未成功获取数据");
                }
            }
        });

    }
    public void getAllTasks(CallBack<List<SystemTask>> callBack, Context context){
        Call call = OkhttpUtil.getRequest("systemTask/getSystemTasksByUserId", context);

        call.enqueue(new Callback() {
            @Override
            public void onFailure(@NonNull Call call, @NonNull IOException e) {
                e.printStackTrace();
                callBack.onFailure(e.getMessage());
            }

            @Override
            public void onResponse(@NonNull Call call, @NonNull Response response) throws IOException {
                String resp = response.body().string();

                Result<List<SystemTask>> result = convertJsonToTaskList(resp);

                if (result != null){
                    if (result.getCode() != null && result.getCode().equals("200")){
                        List<SystemTask> tasks = result.getData();
                        callBack.onSuccess(tasks);
                    }else {
                        callBack.onFailure("result.getCode==null||result.getCode.equals('200'): 获取任务失败");
                    }
                }else {
                    callBack.onFailure("result==null: 未成功获取数据");
                }
            }
        });

    }

    public void isFinishSystemTask(CallBack<Badge> callBack, Context context, SystemTask systemTask){
        String systemTaskJson = new Gson().toJson(systemTask);

        RequestBody requestBody = RequestBody.create(
                MediaType.parse("application/json;charset=UTF-8"),
                systemTaskJson
        );

        Call call = OkhttpUtil.postRequest(requestBody, "systemTask/updateUserSystemTask", context);

        call.enqueue(new Callback() {
            @Override
            public void onFailure(@NonNull Call call, @NonNull IOException e) {
                e.printStackTrace();
                callBack.onFailure(e.getMessage());
            }

            @Override
            public void onResponse(@NonNull Call call, @NonNull Response response) throws IOException {
                String respJson = response.body().string();
                Gson gson = new Gson();

                Result result = gson.fromJson(respJson, Result.class);
                if (result != null){
                    if (result.getCode() != null && result.getCode().equals("200")){
                        Type type = new TypeToken<Result<Badge>>(){}.getType();
                        Result<Badge> badgeResult = new Gson().fromJson(respJson, type);
                        callBack.onSuccess(badgeResult.getData());
                    }else {
                        callBack.onFailure(result.getMessage());
                    }
                }else {
                    callBack.onFailure("更新失败");
                }
            }
        });
    }

    /**
     * 将json转换为Result<List<Task>>类型
     * @param respJson
     * @return
     */
    public Result convertJsonToTaskList(String respJson){
        Gson gson = new Gson();
        Type type = new TypeToken<Result<List<SystemTask>>>() {}.getType();
        Result<List<SystemTask>> result = gson.fromJson(respJson, type);

        return result;
    }
}
